package monsterRun.client.view.popuppanels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import Testing.PlayerMessagesInterface;

import monsterRun.client.view.MonsterFrame;
import monsterRun.common.model.Constants;
import monsterRun.common.model.enums.GameBoardType;
import monsterRun.common.model.enums.GameMode;
import monsterRun.common.model.janimationframework.controllers.PositionAnimationSequence;
import monsterRun.common.model.janimationframework.implementation.AnimationManager;
import monsterRun.common.model.network.Communicator;
import monsterRun.common.model.network.NetworkMessage;
import monsterRun.common.model.network.events.IMessageReceivedListener;
import monsterRun.common.view.Preference;

public class RegistrationPanel  extends JPanel implements ActionListener,
IMessageReceivedListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1302940248939858735L;

	private JButton btnOk;
	private JButton btnCancel;

	private JLabel lblError;
	private JSpinner spinner;
	private MonsterFrame monsterFrame;


	private int animateDistance;
	private AnimationManager animationManager;
	private PositionAnimationSequence seq2;

	private ButtonGroup modeBtnGroup;
	private ButtonGroup boardBtnGroup;

	private JPasswordField passWordField;
	private JTextField firstNameField;
	private JTextField surNameField;
	private JTextField addressField;
	private JTextField phoneField;
	private JTextField userNameField;

	
	private PlayerMessagesInterface pmi;
	
	/** RegistrationPanel Class
	 *  
	 *  This class opens up a registration panel that will ask the user 
	 *  for their details, that will be added into the database. If the fields
	 *  are empty, or if they provide a username that already exists in the database,
	 *  an error will be thrown from the DB Layer that will be passed down as false in
	 *  the client side. The user will then have to provide a different field.
	 *  
	 *  @author s3252820 Gareth Ng 2012
	 */


	public RegistrationPanel(
			PlayerMessagesInterface pmi)
	{
		
		this.pmi = pmi;
		this.animateDistance = 180;

		setPreferredSize(new Dimension(355, 337));

		setLayout(new BorderLayout(0, 0));

		boardBtnGroup = new ButtonGroup();
		modeBtnGroup = new ButtonGroup();

		JPanel buttonsPanel = new JPanel();
		buttonsPanel.setBackground(Preference.FOOTER_BACKGROUND);
		buttonsPanel.setForeground(Preference.FOOTER_FOREGROUND);
		buttonsPanel.setPreferredSize(new Dimension(0, 45));
		add(buttonsPanel, BorderLayout.SOUTH);
		buttonsPanel.setLayout(null);

		btnOk = new JButton("OK");
		btnOk.setOpaque(false);
		btnOk.setBounds(250 - animateDistance, 5, 100, 35);
		btnOk.setFont(Preference.FORMS_FONT);
		btnOk.addActionListener(this);

		buttonsPanel.add(btnOk);

		btnCancel = new JButton("CANCEL");
		btnCancel.setOpaque(false);
		btnCancel.setBounds(170, 5, 100, 35);
		btnCancel.setFont(Preference.FORMS_FONT);
		btnCancel.addActionListener(this);

		buttonsPanel.add(btnCancel);

		JPanel centrePanel = new JPanel();
		add(centrePanel, BorderLayout.CENTER);
		GridBagLayout gbl_centrePanel = new GridBagLayout();
		gbl_centrePanel.columnWidths = new int[] { 20, 151, 87, 0, 20, 0 };
		gbl_centrePanel.rowHeights = new int[] { 20, 0, 0, 37, 0, 0 };
		gbl_centrePanel.columnWeights = new double[] { 1.0, 0.0, 1.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gbl_centrePanel.rowWeights = new double[] { 0.0, 1.0, 1.0, 0.0, 0.0,
				Double.MIN_VALUE };
		centrePanel.setLayout(gbl_centrePanel);

		JLabel passWord = new JLabel("Password: ");
		passWord.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_passWord = new GridBagConstraints();
		gbc_passWord.anchor = GridBagConstraints.NORTHEAST;
		gbc_passWord.insets = new Insets(0, 0, 5, 5);
		gbc_passWord.gridx = 1;
		gbc_passWord.gridy = 6;
		centrePanel.add(passWord, gbc_passWord);

		passWordField = new JPasswordField(10);
		passWordField.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_passWordField = new GridBagConstraints();
		gbc_passWordField.anchor = GridBagConstraints.NORTH;
		gbc_passWordField.insets = new Insets(0, 0, 5, 5);
		gbc_passWordField.gridx = 2;
		gbc_passWordField.gridy = 6;
		centrePanel.add(passWordField, gbc_passWordField);

		JLabel userName = new JLabel("Username: ");
		userName.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_userName = new GridBagConstraints();
		gbc_userName.anchor = GridBagConstraints.NORTHEAST;
		gbc_userName.insets = new Insets(0, 0, 5, 5);
		gbc_userName.gridx = 1;
		gbc_userName.gridy = 5;
		centrePanel.add(userName, gbc_userName);

		userNameField = new JTextField(10);
		userNameField.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_userNameField = new GridBagConstraints();
		gbc_userNameField.anchor = GridBagConstraints.NORTH;
		gbc_userNameField.insets = new Insets(0, 0, 5, 5);
		gbc_userNameField.gridx = 2;
		gbc_userNameField.gridy = 5;
		centrePanel.add(userNameField, gbc_userNameField);

		JLabel phone = new JLabel("Phone: ");
		phone.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_phone = new GridBagConstraints();
		gbc_phone.anchor = GridBagConstraints.NORTHEAST;
		gbc_phone.insets = new Insets(0, 0, 5, 5);
		gbc_phone.gridx = 1;
		gbc_phone.gridy = 4;
		centrePanel.add(phone, gbc_phone);

		phoneField = new JTextField(10);
		phoneField.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_phoneField = new GridBagConstraints();
		gbc_phoneField.anchor = GridBagConstraints.NORTH;
		gbc_phoneField.insets = new Insets(0, 0, 5, 5);
		gbc_phoneField.gridx = 2;
		gbc_phoneField.gridy = 4;
		centrePanel.add(phoneField, gbc_phoneField);

		JLabel address = new JLabel("Address: ");
		address.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_address = new GridBagConstraints();
		gbc_address.anchor = GridBagConstraints.NORTHEAST;
		gbc_address.insets = new Insets(0, 0, 5, 5);
		gbc_address.gridx = 1;
		gbc_address.gridy = 3;
		centrePanel.add(address, gbc_address);

		addressField = new JTextField(10);
		addressField.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_addressField = new GridBagConstraints();
		gbc_addressField.anchor = GridBagConstraints.NORTH;
		gbc_addressField.insets = new Insets(0, 0, 5, 5);
		gbc_addressField.gridx = 2;
		gbc_addressField.gridy = 3;
		centrePanel.add(addressField, gbc_addressField);

		JLabel surName = new JLabel("Sur Name: ");
		surName.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_surName = new GridBagConstraints();
		gbc_surName.anchor = GridBagConstraints.NORTHEAST;
		gbc_surName.insets = new Insets(0, 0, 5, 5);
		gbc_surName.gridx = 1;
		gbc_surName.gridy = 2;
		centrePanel.add(surName, gbc_surName);

		surNameField = new JTextField(10);
		surNameField.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_surNameField = new GridBagConstraints();
		gbc_surNameField.anchor = GridBagConstraints.NORTH;
		gbc_surNameField.insets = new Insets(0, 0, 5, 5);
		gbc_surNameField.gridx = 2;
		gbc_surNameField.gridy = 2;
		centrePanel.add(surNameField, gbc_surNameField);

		JLabel firstName = new JLabel("First Name: ");
		firstName.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_firstName = new GridBagConstraints();
		gbc_firstName.anchor = GridBagConstraints.NORTHEAST;
		gbc_firstName.insets = new Insets(0, 0, 5, 5);
		gbc_firstName.gridx = 1;
		gbc_firstName.gridy = 1;
		centrePanel.add(firstName, gbc_firstName);

		firstNameField = new JTextField(10);
		firstNameField.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		GridBagConstraints gbc_firstNameField = new GridBagConstraints();
		gbc_firstNameField.anchor = GridBagConstraints.NORTH;
		gbc_firstNameField.insets = new Insets(0, 0, 5, 5);
		gbc_firstNameField.gridx = 2;
		gbc_firstNameField.gridy = 1;
		centrePanel.add(firstNameField, gbc_firstNameField);


		JLabel lblLogin = new JLabel("Registration Page:");
		lblLogin.setHorizontalAlignment(SwingConstants.CENTER);
		lblLogin.setOpaque(true);
		lblLogin.setFont(Preference.FORMS_FONT);
		lblLogin.setBackground(Preference.HEADER_BACKGROUND);
		lblLogin.setForeground(Preference.HEADER_FOREGROUND);
		lblLogin.setPreferredSize(new Dimension(0, 45));
		add(lblLogin, BorderLayout.NORTH);

	}

	@Override
	public void messageReceived(String senderId, NetworkMessage message) {
		

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		/** RegistrationPanel Listener
		 *  
		 *  Checks if all fields have been filled, whether chosen username exists in 
		 *  the database, and when registration is successful, bring user back to the
		 *  main page.
		 *  
		 *  @author s3252820 Gareth Ng 2012
		 */

		if(e.getActionCommand() != "CANCEL"){
			
			String str = "";
			char[] c = passWordField.getPassword();
			for (int i= 0 ; i < c.length; i++){
				str = str + c[i];
			}
			//System.out.printf("Password is %s\n", str);
			
			
			if (firstNameField.getText().trim().compareTo("") == 0 || surNameField.getText().trim().compareTo("") == 0 ||
					addressField.getText().trim().compareTo("") == 0 || phoneField.getText().trim().compareTo("") == 0 ||
					userNameField.getText().trim().compareTo("") == 0 || str.trim().compareTo("") == 0){
				
				if (str.trim().compareTo("") == 0){
					JOptionPane.showMessageDialog(this,
						    "Password cannot be pure spaces\nPlease fill in ALL fields in this form",
						    "Missing Field Detected / Password Error",
						    JOptionPane.ERROR_MESSAGE);
					return;
				}

				JOptionPane.showMessageDialog(this,
					    "Please fill in ALL fields in this form",
					    "Missing Field Detected",
					    JOptionPane.ERROR_MESSAGE);
				return;
			}
			
//			System.out.printf("First name is %s\n", firstNameField.getText());
//			System.out.printf("Sur name is %s\n", surNameField.getText());
//			System.out.printf("Address is %s\n", addressField.getText());
//			System.out.printf("Phone is %s\n", phoneField.getText());
//			System.out.printf("Username is %s\n", userNameField.getText());
//			System.out.printf("Password is %s\n", str);

			try
			{
				String[] s = {firstNameField.getText(),surNameField.getText(),
						addressField.getText(),phoneField.getText(),
						userNameField.getText(),str};
				if (!pmi.createRegistrationRequest(s)){
					JOptionPane.showMessageDialog(this,
						    "This username has been taken\nPlease choose a different one",
						    "Unique Username Identified",
						    JOptionPane.ERROR_MESSAGE);
					return;
				}else{
					JOptionPane.showMessageDialog(this,
				    "Registration Successful! Please log in");
					monsterFrame.hidePopups();
				}
			}
			catch (RemoteException e1)
			{
				e1.printStackTrace();
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			}
			
		} else {
			monsterFrame.hidePopups();
		}
	}

	public void setMonsterFrame(MonsterFrame monsterFrame)
	{
		this.monsterFrame = monsterFrame;
	}

}
